﻿using Prism.Navigation.Model;
using Prism.Navigation.Msg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace Prism.Navigation.ViewModels
{
    public class ShellViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        private readonly IEventAggregator _eventAggregator;

        public ShellViewModel(IRegionManager regionManager,IEventAggregator eventAggregator)
        {
            _regionManager=regionManager;
            _eventAggregator=eventAggregator;

            //延迟执行 InitSubscribe() 和 OpenDefaultWindow() 这两个方法，直到 WPF 窗口的加载过程进行到特定阶段之后。
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                InitSubscribe();
                OpenDefaultWindow();
            }), DispatcherPriority.Loaded);
        }

        private void InitSubscribe()
        {
            //接收登录
            _eventAggregator.GetEvent<LoginMessage>().Subscribe((message) =>
            {
                _regionManager.Regions["ContentRegion"].RequestNavigate("MainView");
                //让窗口变大
                WindowSize windowSize =new WindowSize() { Width=1200, Height = 800};
                _eventAggregator.GetEvent<ChangeWindowSizeMessage>().Publish(windowSize);
            });

            //接收登出
            _eventAggregator.GetEvent<LoginOutMessage>().Subscribe((message) =>
            {
                _regionManager.Regions["ContentRegion"].RequestNavigate("LoginView");
                WindowSize windowSize = new WindowSize() { Width = 600, Height = 450 };
                _eventAggregator.GetEvent<ChangeWindowSizeMessage>().Publish(windowSize);
            });
        }

        private void OpenDefaultWindow()
        {
            _regionManager.Regions["ContentRegion"].RequestNavigate("LoginView");
        }

        
    }
}
